<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/18
 * Time: 17:51
 */
namespace App\MultiTrait;

use App\Constant\ErrorConstant;

trait ResponseTrait
{
    /**
     * 成功请求返回
     * @param array $data
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    public function successResponse($data = [], $msg = '请求成功')
    {
        return response()->json([
            'code'  =>  0,
            'msg'   =>  $msg,
            'data'  =>  $data
        ]);
    }

    /**
     * 错误请求返回
     * @param int $code
     * @param string|null $msg
     * @return \Illuminate\Http\JsonResponse
     */
    public function errorResponse($code = ErrorConstant::PARAM_ERROR, $msg = Null)
    {
        return response()->json([
            'code'  =>  $code,
            'msg'   =>  $msg ?? (ErrorConstant::ERROR_DESC[$code] ?? '请求错误'),
            'data'  =>  []
        ]);
    }
}
